var express = require("express");
var router = express.Router();
var {
  Users,
  Category,
  Chooses,
  Paper,
  Experience,
  Advise,
} = require("../utils/model");
var {
  Users,
  Category,
  Chooses,
  Paper,
  Experience,
  Announce,
} = require("../utils/model");
var axios = require("axios");
const { createToken, checkToken } = require("../utils/token");
let count = 1;
router.get("/test", (req, res) => {
  res.json({
    code: 200,
    msg: "test  react 项目的 接口 test",
  });
});
// 登录
router.post("/login", (req, res) => {
  var body = req.body;
  // console.log(body);
  Users.findOne(body)
    .then((value) => {
      var token = createToken(body);
      if (value) {
        res.json({
          code: 200,
          msg: "登录成功",
          value,
          token,
        });
      } else {
        res.json({
          code: 401,
          msg: "登录失败",
        });
      }
    })
    .catch((err) => {
      res.json({
        code: 500,
        msg: "服务器异常",
        value,
      });
    });
});
// 发送验证码
router.post("/sendCaptcha", (req, res) => {
  var body = req.body;

  Users.findOne(body)
    .then((value) => {
      if (value) {
        axios
          .get("http://47.104.209.44:3333/captcha/sent", {
            params: {
              phone: body.phone,
            },
          })
          .then((val) => {
            res.json({
              code: 200,
              msg: "发送成功",
              value,
            });
          });
      } else {
        res.json({
          code: 401,
          msg: "发送失败,请检测手机号",
        });
      }
    })
    .catch((err) => {
      res.json({
        code: 500,
        msg: "服务器异常",
        value,
      });
    });
});
// 效验验证码登录
router.post("/checkcaptcha", (req, res) => {
  var body = req.body;
  // console.log(body);
  Users.findOne({ phone: body.phone })
    .then((value) => {
      if (value) {
        axios
          .get("http://47.104.209.44:3333/captcha/verify", {
            params: {
              phone: body.phone,
              captcha: body.captcha,
            },
          })
          .then((val) => {
            var token = createToken(body);
            res.json({
              code: 200,
              msg: "登录成功",
              token,
              ...val.data,
            });
          });
      } else {
        res.json({
          code: 401,
          msg: "登录失败,请检测手机号",
        });
      }
    })
    .catch((err) => {
      res.json({
        code: 500,
        msg: "服务器异常",
        err,
      });
    });
});
// 注册
router.post("/register", (req, res) => {
  var body = req.body;
  // console.log(body);
  Users.findOne({
    phone: body.phone,
  }).then((value) => {
    // console.log(value);
    if (value) {
      res.json({
        code: 401,
        msg: "手机号已存在",
      });
    } else {
      (body.role = 1),
        (body.username = "用户" + Math.floor(Math.random() * 100000));
      Users.insertMany(body).then((val) => {
        res.json({
          code: 200,
          msg: "注册成功,即将跳转",
          val,
        });
      });
    }
  });
});
// 获取用户信息
router.get("/userlist", (req, res) => {
  checkToken(req, res, (data) => {
    Users.findOne({ phone: data.phone })
      .then((val) => {
        console.log(val);
        res.json({
          code: 200,
          msg: "查询成功",
          val,
        });
      })
      .catch((err) => {
        res.json({
          code: 501,
          msg: "服务器异常",
          err,
        });
      });
  });
});
// 获取全部用户信息
router.get("/userall", (req, res) => {
  checkToken(req, res, (data) => {
    Users.find()
      .then((val) => {
        console.log(val);
        res.json({
          code: 200,
          msg: "查询成功",
          val,
        });
      })
      .catch((err) => {
        res.json({
          code: 501,
          msg: "服务器异常",
          err,
        });
      });
  });
});

// 上传头像
var multer = require("multer");
var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, "./public/avatar");
  },
  filename: function (req, file, cb) {
    cb(null, "WH2101" + Date.now() + file.originalname);
  },
});
var upload = multer({ storage: storage }).any(); // 不限文件格式  不限数据
// 头像上传
router.post("/sendavatar", upload, (req, res) => {
  var path = req.files[0].path;
  checkToken(req, res, (data) => {
    Users.updateMany(
      {
        phone: data.phone,
      },
      {
        $set: {
          avatar: path,
        },
      }
    )
      .then((result) => {
        // 存储 users
        res.json({
          code: 200,
          msg: "头像上传成功",
          path,
          result,
        });
      })
      .catch((err) => {
        res.json({
          code: 500,
          msg: "服务器异常",
          err,
        });
      });
  });
});

//上传分类
router.post("/category", (req, res) => {
  var body = req.body;

  Category.findOne({
    code: body.code,
  }).then((value) => {
    if (value) {
      res.json({
        code: 401,
        msg: "该分类已存在",
      });
    } else {
      Category.insertMany(body).then((val) => {
        res.json({
          code: 200,
          msg: "增加成功！",
          val,
        });
      });
    }
  });
});

//文件上传
router.post("/sendpic", upload, (req, res) => {
  var path = req.files[0].path;
  //   checkToken((req, res, data) => {
  res.json({
    code: 200,
    msg: "图片上传成功",
    path,
  });
  //   });
});

//获取分类数据
router.get("/categorylist", (req, res) => {
  checkToken(req, res, (data) => {
    Category.find()
      .then((val) => {
        res.json({
          code: 200,
          msg: "查询成功",
          val,
        });
      })
      .catch((err) => {
        res.json({
          code: 501,
          msg: "服务器异常",
          err,
        });
      });
  });
});

//修改分类数据
router.post("/updatecategory", (req, res) => {
  var body = req.body;
  checkToken(req, res, (data) => {
    Category.findOne({
      code: body.code,
    })
      .then((val) => {
        Category.updateMany(
          {
            _id: body._id,
          },
          {
            $set: body,
          }
        )
          .then((result) => {
            res.json({
              code: 200,
              msg: "修改成功",
              result,
            });
          })
          .catch((err) => {
            res.json({
              code: 500,
              msg: "服务器异常",
              err,
            });
          });
      })
      .catch((err) => {
        res.json({
          code: 500,
          msg: "服务器异常",
          err,
        });
      });
  });
});

//删除分类
router.post("/category-delete", (req, res) => {
  var body = req.body;

  checkToken(req, res, (data) => {
    Category.remove({
      _id: body._id,
    })
      .then((result) => {
        res.json({
          code: 200,
          msg: "删除成功",
          result,
        });
      })
      .catch((err) => {
        res.json({
          code: 500,
          msg: "服务器异常",
          err,
        });
      });
  });
});

// 添加选择题
router.post("/addchoose", (req, res) => {
  var body = req.body;
  checkToken(req, res, (data) => {
    body.time = new Date();
    body.correct = body.correct.toUpperCase();
    body.phone = data.phone;
    body.status = 0;
    body.heat = 0;
    (body.shouchang = 0),
      (body.dianzan = 0),
      Chooses.insertMany(body)
        .then((val) => {
          res.json({
            code: 200,
            msg: "添加成功",
          });
        })
        .catch((err) => {
          res.json({
            code: 500,
            msg: "服务器异常",
          });
        });
  });
});
// 添加套题
router.post("/addpaper", (req, res) => {
  var body = req.body;
  checkToken(req, res, (data) => {
    body.heat = 0;
    (body.shouchang = 0), (body.dianzan = 0), (body.subjects = []);
    Paper.insertMany(body)
      .then((val) => {
        res.json({
          code: 200,
          msg: "添加成功",
        });
      })
      .catch((err) => {
        res.json({
          code: 500,
          msg: "服务器异常",
        });
      });
  });
});
// 获取未审核的选择题
router.post("/getchoose", (req, res) => {
  var body = req.body;
  checkToken(req, res, (data) => {
    Chooses.find({
      status: body.status,
    })
      .then((val) => {
        res.json({
          code: 200,
          msg: "查询成功",
          val,
        });
      })
      .catch((err) => {
        res.json({
          code: 500,
          msg: "服务器异常",
          err,
        });
      });
  });
});
// 通过选择题
router.post("/passchoose", (req, res) => {
  var body = req.body;
  checkToken(req, res, (data) => {
    Chooses.updateMany(
      {
        _id: body._id,
      },
      {
        $set: {
          status: 1,
        },
      }
    )
      .then((val) => {
        res.json({
          code: 200,
          msg: "审核成功",
          val,
        });
      })
      .catch((err) => {
        res.json({
          code: 500,
          msg: "服务器异常",
          err,
        });
      });
  });
});

// 删除选择题
router.post("/deletechoose", (req, res) => {
  var body = req.body;
  checkToken(req, res, (data) => {
    Chooses.remove({
      _id: body._id,
    })
      .then((val) => {
        res.json({
          code: 200,
          msg: "删除成功",
          val,
        });
      })
      .catch((err) => {
        res.json({
          code: 500,
          msg: "服务器异常",
          err,
        });
      });
  });
});

// 更新用户信息
router.post("/updateuser", (req, res) => {
  var body = req.body;
  checkToken(req, res, (data) => {
    Users.updateMany(
      {
        _id: body._id,
      },
      {
        $set: {
          ...body,
        },
      }
    )
      .then((val) => {
        res.json({
          code: 200,
          msg: "更新成功",
          val,
        });
      })
      .catch((err) => {
        res.json({
          code: 500,
          msg: "服务器异常",
          err,
        });
      });
  });
});

// 面经查询头像

router.post("/getuserinfo", (req, res) => {
  var body = req.body;
  checkToken(req, res, (data) => {
    Users.find({ phone: body.phone })
      .then((val) => {
        res.json({
          code: 200,
          msg: "查询成功",
          val,
        });
      })
      .catch((err) => {
        res.json({
          code: 501,
          msg: "服务器异常",
          err,
        });
      });
  });
});

// 搜索套题
router.post("/searchtaotilist", (req, res) => {
  var body = req.body;
  checkToken(req, res, (data) => {
    Paper.find({
      title: new RegExp(body.str),
    })
      .then((val) => {
        Paper.find({
          technology: new RegExp(body.str),
        }).then((result) => {
          res.json({
            code: 200,
            msg: "查询成功",
            val,
            result,
          });
        });
      })
      .catch((err) => {
        res.json({
          code: 501,
          msg: "服务器异常",
          err,
        });
      });
  });
});

// 往套题里添加选择题
router.post("/taoaddchoose", (req, res) => {
  var body = req.body;

  checkToken(req, res, (data) => {
    Paper.updateMany(
      {
        _id: body._id,
      },
      {
        $set: {
          subjects: body.subjects,
        },
      }
    )
      .then((val) => {
        res.json({
          code: 200,
          msg: "添加成功",
          val,
        });
      })
      .catch((err) => {
        res.json({
          code: 501,
          msg: "服务器异常",
          err,
        });
      });
  });
});
// 排序选择题
router.post("/sortchoose", (req, res) => {
  var body = req.body;
  checkToken(req, res, (data) => {
    Chooses.find({
      code: body.code,
      status: body.status,
    })
      .sort({ heat: 1 })
      .then((val) => {
        res.json({
          code: 200,
          msg: "排序成功",
          val,
        });
      })
      .catch((err) => {
        res.json({
          code: 501,
          msg: "服务器异常",
          err,
        });
      });
  });
});
// 排序选择题
router.post("/sorttaoti", (req, res) => {
  var body = req.body;
  checkToken(req, res, (data) => {
    Paper.find({
      code: body.code,
    })
      .sort({ heat: 1 })
      .then((val) => {
        res.json({
          code: 200,
          msg: "排序成功",
          val,
        });
      })
      .catch((err) => {
        res.json({
          code: 501,
          msg: "服务器异常",
          err,
        });
      });
  });
});

//增加面试经验
router.post("/experience", (req, res) => {
  var body = req.body;
  console.log(body);
  Experience.insertMany(body)
    .then((val) => {
      res.json({
        code: 200,
        msg: "增加成功！",
        val,
      });
    })
    .catch((error) => {
      res.json({
        code: 500,
        msg: "服务器异常",
      });
    });
});

//删除面试经验
router.post("/experience-delete", (req, res) => {
  var body = req.body;
  console.log(body._id);
  checkToken(req, res, (data) => {
    Experience.remove({
      _id: body._id,
    })
      .then((result) => {
        res.json({
          code: 200,
          msg: "删除成功",
          result,
        });
      })
      .catch((err) => {
        res.json({
          code: 500,
          msg: "服务器异常",
          err,
        });
      });
  });
});

//修改面试经验
router.post("/updateExperience", (req, res) => {
  var body = req.body;
  console.log(body);
  checkToken(req, res, (data) => {
    Experience.updateMany(
      {
        _id: body._id,
      },
      {
        $set: body,
      }
    )
      .then((result) => {
        res.json({
          code: 200,
          msg: "修改成功",
          result,
        });
      })
      .catch((err) => {
        res.json({
          code: 500,
          msg: "服务器异常",
          err,
        });
      });
  });
});

//查询面试数据
router.post("/experiencelist", (req, res) => {
  var body = req.body;
  checkToken(req, res, (data) => {
    Experience.find(body)
      .then((val) => {
        // console.log(val);
        res.json({
          code: 200,
          msg: "查询成功",
          val,
        });
      })
      .catch((err) => {
        res.json({
          code: 501,
          msg: "服务器异常",
          err,
        });
      });
  });
});

// 面经查询头像

router.post("/getuserinfo", (req, res) => {
  var body = req.body;
  console.log(body);
  checkToken(req, res, (data) => {
    Users.find(body)
      .then((val) => {
        res.json({
          code: 200,
          msg: "查询成功",
          val,
        });
      })
      .catch((err) => {
        res.json({
          code: 501,
          msg: "服务器异常",
          err,
        });
      });
  });
});

// 搜索选择题
router.post("/searchchoose", (req, res) => {
  var body = req.body;
  checkToken(req, res, (data) => {
    Chooses.find({
      subject: new RegExp(body.str),
      status: body.status,
    })
      .then((val) => {
        Chooses.find({
          technology: new RegExp(body.str),
          status: body.status,
        }).then((result) => {
          res.json({
            code: 200,
            msg: "查询成功",
            val,
            result,
          });
        });
      })
      .catch((err) => {
        res.json({
          code: 501,
          msg: "服务器异常",
          err,
        });
      });
  });
});

// 面经点赞
router.post("/addstar", (req, res) => {
  var body = req.body;
  var heat = body.heat + 1;

  checkToken(req, res, (data) => {
    Experience.updateMany(
      {
        _id: body._id,
      },
      {
        $set: {
          star: body.star,
          heat: heat,
        },
      }
    )
      .then((val) => {
        Users.updateMany(
          {
            phone: data.phone,
          },
          {
            $push: {
              starArr: body._id,
            },
          }
        ).then((result) => {
          res.json({
            code: 200,
            msg: "点赞成功",
            val,
            result,
          });
        });
      })
      .catch((err) => {
        res.json({
          code: 500,
          msg: "服务器错误",
          err,
        });
      });
  });
});

// 面经取消点赞
router.post("/delstar", (req, res) => {
  var body = req.body;
  var heat = body.heat - 1;
  checkToken(req, res, (data) => {
    Experience.updateMany(
      {
        _id: body._id,
      },
      {
        $set: {
          star: body.star,
          heat: heat,
        },
      }
    )
      .then((val) => {
        Users.updateMany(
          {
            phone: data.phone,
          },
          {
            $pull: {
              starArr: body._id,
            },
          }
        ).then((result) => {
          res.json({
            code: 200,
            msg: "取消点赞成功",
            val,
            result,
          });
        });
      })
      .catch((err) => {
        res.json({
          code: 500,
          msg: "服务器错误",
          err,
        });
      });
  });
});

// 面经收藏
router.post("/addcollect", (req, res) => {
  var body = req.body;
  var heat = body.heat + 2;

  checkToken(req, res, (data) => {
    Experience.updateMany(
      {
        _id: body._id,
      },
      {
        $set: {
          collect: body.collect,
          heat: heat,
        },
      }
    )
      .then((val) => {
        Users.updateMany(
          {
            phone: data.phone,
          },
          {
            $push: {
              collectArr: body._id,
            },
          }
        ).then((result) => {
          res.json({
            code: 200,
            msg: "收藏成功",
            val,
            result,
          });
        });
      })
      .catch((err) => {
        res.json({
          code: 500,
          msg: "服务器错误",
          err,
        });
      });
  });
});

// 面经取消点赞
router.post("/delcollect", (req, res) => {
  var body = req.body;
  var heat = body.heat - 2;
  checkToken(req, res, (data) => {
    Experience.updateMany(
      {
        _id: body._id,
      },
      {
        $set: {
          collect: body.collect,
          heat: heat,
        },
      }
    )
      .then((val) => {
        Users.updateMany(
          {
            phone: data.phone,
          },
          {
            $pull: {
              collectArr: body._id,
            },
          }
        ).then((result) => {
          res.json({
            code: 200,
            msg: "取消收藏成功",
            val,
            result,
          });
        });
      })
      .catch((err) => {
        res.json({
          code: 500,
          msg: "服务器错误",
          err,
        });
      });
  });
});

//添加公告
router.post("/addAnnounces", (req, res) => {
  var body = req.body;
  //   console.log(body);
  Users.find({
    phone: body.phone,
  }).then((result) => {
    console.log(result[0]);
    Announce.insertMany({
      ...body,
      avatar: result[0].avatar,
      username: result[0].username,
    })
      .then((val) => {
        res.json({
          code: 200,
          msg: "增加成功！",
          val,
        });
      })
      .catch((error) => {
        res.json({
          code: 500,
          msg: "服务器异常",
        });
      });
  });
});

//查询公告
router.post("/announceList", (req, res) => {
  var body = req.body;
  checkToken(req, res, (data) => {
    Announce.find(body)
      .then((val) => {
        // console.log(val);
        res.json({
          code: 200,
          msg: "查询成功",
          val,
        });
      })
      .catch((err) => {
        res.json({
          code: 501,
          msg: "服务器异常",
          err,
        });
      });
  });
});

//修改公告
router.post("/updateAnnounce", (req, res) => {
  var body = req.body;
  console.log(body);
  checkToken(req, res, (data) => {
    Announce.updateMany(
      {
        _id: body._id,
      },
      {
        $set: body,
      }
    )
      .then((result) => {
        res.json({
          code: 200,
          msg: "修改成功",
          result,
        });
      })
      .catch((err) => {
        res.json({
          code: 500,
          msg: "服务器异常",
          err,
        });
      });
  });
});

//删除公告
router.post("/announceDelete", (req, res) => {
  var body = req.body;
  console.log(body._id);
  checkToken(req, res, (data) => {
    Announce.remove({
      _id: body._id,
    })
      .then((result) => {
        res.json({
          code: 200,
          msg: "删除成功",
          result,
        });
      })
      .catch((err) => {
        res.json({
          code: 500,
          msg: "服务器异常",
          err,
        });
      });
  });
});
// 点赞
router.post("/adddianzan", (req, res) => {
  var body = req.body;
  var heat = body.heat + 1;
  var dianzan = ++body.dianzan;
  console.log(body, 1);
  checkToken(req, res, (data) => {
    console.log(body, 2);
    if (body.type == 1) {
      Chooses.updateMany(
        {
          _id: body._id,
        },
        {
          $set: {
            dianzan: dianzan,
            heat: heat,
          },
        }
      )
        .then((val) => {
          Users.updateMany(
            {
              phone: data.phone,
            },
            {
              $push: {
                starArr: body._id,
              },
            }
          ).then((result) => {
            res.json({
              code: 200,
              msg: "点赞成功",
              val,
              result,
            });
          });
        })
        .catch((err) => {
          res.json({
            code: 500,
            msg: "服务器错误",
            err,
          });
        });
    } else if (body.type == 2) {
      Paper.updateMany(
        {
          _id: body._id,
        },
        {
          $set: {
            dianzan: ++body.dianzan,
            heat: heat,
          },
        }
      )
        .then((val) => {
          Users.updateMany(
            {
              phone: data.phone,
            },
            {
              $push: {
                starArr: body._id,
              },
            }
          ).then((result) => {
            res.json({
              code: 200,
              msg: "点赞成功",
              val,
              result,
            });
          });
        })
        .catch((err) => {
          res.json({
            code: 500,
            msg: "服务器错误",
            err,
          });
        });
    }
  });
});
// 取消点赞
router.post("/deletedianzan", (req, res) => {
  var body = req.body;
  var heat = body.heat - 1;
  checkToken(req, res, (data) => {
    if (body.type == 1) {
      Chooses.updateMany(
        {
          _id: body._id,
        },
        {
          $set: {
            dianzan: --body.dianzan,
            heat: heat,
          },
        }
      )
        .then((val) => {
          Users.updateMany(
            {
              phone: data.phone,
            },
            {
              $pull: {
                starArr: body._id,
              },
            }
          ).then((result) => {
            res.json({
              code: 200,
              msg: "取消点赞",
              val,
              result,
            });
          });
        })
        .catch((err) => {
          res.json({
            code: 500,
            msg: "服务器错误",
            err,
          });
        });
    } else if (body.type == 2) {
      Paper.updateMany(
        {
          _id: body._id,
        },
        {
          $set: {
            dianzan: ++body.dianzan,
            heat: heat,
          },
        }
      )
        .then((val) => {
          Users.updateMany(
            {
              phone: data.phone,
            },
            {
              $pull: {
                starArr: body._id,
              },
            }
          ).then((result) => {
            res.json({
              code: 200,
              msg: "取消成功",
              val,
              result,
            });
          });
        })
        .catch((err) => {
          res.json({
            code: 500,
            msg: "服务器错误",
            err,
          });
        });
    }
  });
});

// 收藏
router.post("/addshouchang", (req, res) => {
  var body = req.body;
  var heat = body.heat + 2;
  var shouchang = ++body.shouchang;
  checkToken(req, res, (data) => {
    if (body.type == 1) {
      Chooses.updateMany(
        {
          _id: body._id,
        },
        {
          $set: {
            shouchang: shouchang,
            heat: heat,
          },
        }
      )
        .then((val) => {
          Users.updateMany(
            {
              phone: data.phone,
            },
            {
              $push: {
                collectArr: body._id,
              },
            }
          ).then((result) => {
            res.json({
              code: 200,
              msg: "收藏成功",
              val,
              result,
            });
          });
        })
        .catch((err) => {
          res.json({
            code: 500,
            msg: "服务器错误",
            err,
          });
        });
    } else if (body.type == 2) {
      Paper.updateMany(
        {
          _id: body._id,
        },
        {
          $set: {
            shouchang: ++body.shouchang,
            heat: heat,
          },
        }
      )
        .then((val) => {
          Users.updateMany(
            {
              phone: data.phone,
            },
            {
              $push: {
                collectArr: body._id,
              },
            }
          ).then((result) => {
            res.json({
              code: 200,
              msg: "收藏成功",
              val,
              result,
            });
          });
        })
        .catch((err) => {
          res.json({
            code: 500,
            msg: "服务器错误",
            err,
          });
        });
    }
  });
});
// 取消收藏
router.post("/deleteshouchang", (req, res) => {
  var body = req.body;
  var heat = body.heat - 2;
  checkToken(req, res, (data) => {
    if (body.type == 1) {
      Chooses.updateMany(
        {
          _id: body._id,
        },
        {
          $set: {
            shouchang: --body.shouchang,
            heat: heat,
          },
        }
      )
        .then((val) => {
          Users.updateMany(
            {
              phone: data.phone,
            },
            {
              $pull: {
                collectArr: body._id,
              },
            }
          ).then((result) => {
            res.json({
              code: 200,
              msg: "取消收藏",
              val,
              result,
            });
          });
        })
        .catch((err) => {
          res.json({
            code: 500,
            msg: "服务器错误",
            err,
          });
        });
    } else if (body.type == 2) {
      Paper.updateMany(
        {
          _id: body._id,
        },
        {
          $set: {
            shouchang: --body.shouchang,
            heat: heat,
          },
        }
      )
        .then((val) => {
          Users.updateMany(
            {
              phone: data.phone,
            },
            {
              $pull: {
                collectArr: body._id,
              },
            }
          ).then((result) => {
            res.json({
              code: 200,
              msg: "取消成功",
              val,
              result,
            });
          });
        })
        .catch((err) => {
          res.json({
            code: 500,
            msg: "服务器错误",
            err,
          });
        });
    }
  });
});
// 获取套题详细数据
router.post("/gettaotiDetails", (req, res) => {
  var body = req.body;
  checkToken(req, res, (data) => {
    Paper.findOne({ _id: body._id })
      .then((val) => {
        res.json({
          code: 200,
          msg: "获取成功",
          val,
        });
      })
      .catch((err) => {
        res.json({
          code: 500,
          msg: "服务器错误",
          err,
        });
      });
  });
});

// 获取点赞列表
router.post("/getstarlist", (req, res) => {
  var body = req.body;
  // console.log(body);
  var temp = [];
  checkToken(req, res, (data) => {
    if (body.type == 1) {
      body.arr.forEach((item) => {
        Experience.findOne({ _id: item }).then((val) => {
          if (val && val._id) {
            temp.push(val);
          }
        });
      });
    } else if (body.type == 2) {
      body.arr.forEach((item) => {
        Paper.findOne({ _id: item }).then((val) => {
          if (val && val._id) {
            temp.push(val);
          }
        });
      });
    } else if (body.type == 3) {
      body.arr.forEach((item) => {
        Chooses.findOne({ _id: item }).then((val) => {
          if (val && val._id) {
            temp.push(val);
          }
        });
      });
    }
    setTimeout(() => {
      if (temp) {
        res.json({
          code: 200,
          msg: "获取成功",
          temp,
        });
      } else {
        res.json({
          code: 304,
          msg: "暂无数据",
        });
      }
    }, 100);
  });
});

// 修改个人信息
router.post("/updatepersonal", (req, res) => {
  var body = req.body;
  checkToken(req, res, (data) => {
    Users.updateMany(
      { phone: data.phone },
      {
        $set: {
          ...body,
        },
      }
    )
      .then((val) => {
        res.json({
          code: 200,
          msg: "更新成功",
          val,
        });
      })
      .catch((err) => {
        res.json({
          code: 500,
          msg: "服务器异常",
          err,
        });
      });
  });
});

// 添加反馈意见

router.post("/addadvise", (req, res) => {
  var body = req.body;
  var time = new Date();

  body.time = time;
  body.status = 1;
  checkToken(req, res, (data) => {
    Advise.insertMany(body)
      .then((val) => {
        res.json({
          code: 200,
          msg: "提交成功",
          val,
        });
      })
      .catch((err) => {
        res.json({
          code: 500,
          msg: "服务器异常",
          err,
        });
      });
  });
});
// 获取反馈列表
router.post("/getadviselist", (req, res) => {
  checkToken(req, res, (data) => {
    Advise.find()
      .then((val) => {
        res.json({
          code: 200,
          msg: "获取成功",
          val,
        });
      })
      .catch((err) => {
        res.json({
          code: 500,
          msg: "服务器异常",
          err,
        });
      });
  });
});
// 更新反馈列表
router.post("/updateadviselist", (req, res) => {
  var body = req.body;
  checkToken(req, res, (data) => {
    Advise.updateMany(
      { _id: body._id },
      {
        $set: {
          status: 2,
        },
      }
    )
      .then((val) => {
        res.json({
          code: 200,
          msg: "审核通过",
          val,
        });
      })
      .catch((err) => {
        res.json({
          code: 500,
          msg: "服务器异常",
          err,
        });
      });
  });
});
// 删除反馈列表
router.post("/delateadviselist", (req, res) => {
  var body = req.body;
  checkToken(req, res, (data) => {
    Advise.remove({ _id: body._id })
      .then((val) => {
        res.json({
          code: 200,
          msg: "驳回成功",
          val,
        });
      })
      .catch((err) => {
        res.json({
          code: 500,
          msg: "服务器异常",
          err,
        });
      });
  });
});

// 搜索反馈列表
router.post("/searchadviselist", (req, res) => {
  var body = req.body;
  var temp = [];
  checkToken(req, res, (data) => {
    Advise.find({
      title: new RegExp(body.value),
    }).then((val) => {
      Advise.find({
        content: new RegExp(body.value),
      })
        .then((result) => {
          temp = val.concat(result);
          res.json({
            code: 200,
            msg: "查询成功",
            temp,
          });
        })
        .catch((err) => {
          res.json({
            code: 500,
            msg: "服务器异常",
            err,
          });
        });
    });
  });
});
module.exports = router;
